package gemini.castor.ui.client.page.content.home.apply.distributorid;

import gemini.basic.model.Person;
import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.ErrorWidget;
import gemini.castor.ui.client.page.widget.TextFieldTextNumber;
import gemini.castor.ui.client.page.widget.distributorid.DistributorIdWidget;

import java.util.Iterator;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.validation.client.InvalidConstraint;
import com.google.gwt.validation.client.interfaces.IValidator;

public class DistributorIDFormView extends Composite implements
		DistributorIDFormPresenter.Display {

	@UiTemplate("DistributorIDFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, DistributorIDFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	private DistributorIDFormObject object;
	private IValidator<DistributorIDFormObject> validator;
	private I18nConstants constants;

	public DistributorIDFormView() {
		initWidget(binder.createAndBindUi(this));
		validator = GWT.create(DistributorIDFormObject.class);
		constants = GWT.create(I18nConstants.class);
	}

	@Override
	public boolean isValidate() {
		resetStyle();
		bind();
		boolean result = true;
		Set<InvalidConstraint<DistributorIDFormObject>> invalidConstraints = validator
				.validate(object);
		Iterator<InvalidConstraint<DistributorIDFormObject>> it = invalidConstraints
				.iterator();

		while (it.hasNext()) {
			if (result) {
				result = false;
			}
			InvalidConstraint<DistributorIDFormObject> ic = it.next();
			showError(ic.getItemName(), ic.getMessage());
		}

		boolean validSponsor = swgSponsor.isValidate();
		
		boolean validPin = isValidPin();

		boolean allValid = result && validSponsor&&validPin;
		if (!allValid) {
			cnwError.setVisible(true);
			
			if(!validPin){
				cnwError.setMessage(constants.pinRetypePinNotEqual());
			} else {
				cnwError.setMessage(constants.emptyField());
			}
		}

		return allValid;
	}

	private boolean isValidPin() {
		boolean result = true;
		if (!object.getPin().isEmpty() && !object.getRetypePin().isEmpty()
				&& !object.getPin().equals(object.getRetypePin())) {
			showError("pin", "");
			showError("retypePin", "");
			result = false;
		}
		return result;
	}

	private void showError(String widgetName, String errorString) {
		if (widgetName.equals("pin")) {
			pbxPin.addStyleDependentName("error");
		} else if (widgetName.equals("retypePin")) {
			pbxRetypePin.addStyleDependentName("error");
		}
	}

	private void bind() {
		object.setPin(pbxPin.getValue());
		object.setRetypePin(pbxRetypePin.getValue());
	}

	@UiHandler("btnReset")
	void onReset(ClickEvent e) {
		reset();
	}

	private void reset() {
		resetValue();
		resetStyle();
		swgSponsor.reset();
		cnwError.setVisible(false);
	}
	
	private void resetValue(){
		pbxPin.setValue("");
		pbxRetypePin.setValue("");
	}
	
	private void resetStyle() {		
		pbxPin.removeStyleDependentName("error");
		pbxRetypePin.removeStyleDependentName("error");
	}

	@UiField
	ErrorWidget cnwError;
	
	@UiField
	PasswordTextBox pbxPin;
	
	@UiField
	PasswordTextBox pbxRetypePin;
	
	@UiField
	TextBox tbxFullName;

	@UiField
	TextFieldTextNumber tbxPersonalId;

	@UiField
	TextBox tbxSpouseFullName;
	
	@UiField
	TextFieldTextNumber tbxSpousePersonalId;

	@UiField
	TextFieldTextNumber tbxDistributorId;

	@UiField
	DistributorIdWidget swgSponsor;

	@UiField
	ListBox lbxDistributorId;
	
	@UiField
	HTMLPanel pnlSpouseFullName;
	
	@UiField
	HTMLPanel pnlSpousePersonalId;

	@UiField
	Button btnSubmit;

	@UiField
	Button btnCancel;

	@UiField
	Button btnReset;

	@Override
	public void loadData() {
		swgSponsor.setDistributorIdObject(object.getSponsor());

		lbxDistributorId.addItem("Vietnam");
		tbxDistributorId.setValue(object.getCode());

		Person ps = object.getPersons().get(0);
		tbxPersonalId.setValue(ps.getId().toString(), true);
		tbxFullName.setValue(ps.getFirstName() + " " + ps.getLastName());
		
		if (object.getPersons().size() > 1) {
			Person spouse = object.getPersons().get(1);
			tbxSpousePersonalId.setValue(spouse.getId().toString(), true);
			tbxSpouseFullName.setValue(spouse.getFirstName() + " " + spouse.getLastName());
			
			pnlSpouseFullName.setVisible(true);
			pnlSpousePersonalId.setVisible(true);
		} else {
			pnlSpouseFullName.setVisible(false);
			pnlSpousePersonalId.setVisible(false);
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasClickHandlers getDistributorSubmitClickHandlers() {
		return btnSubmit;
	}

	@Override
	public HasClickHandlers getCancelClickHandlers() {
		return btnCancel;
	}

	@Override
	public void setObject(DistributorIDFormObject inputObject) {
		object = inputObject;
	}
	
	@Override
	public DistributorIDFormObject getObject() {
		return object;
	}
}
